package com.rybbaby.tss.front.tssMemberJoinPakcage.controller;

import com.google.common.collect.ImmutableMap;
import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.PropertiesReadUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssMemberJoinPakcage.entity.TssMemberJoinPackageBean;
import com.rybbaby.tss.front.tssMemberJoinPakcage.service.ITssMemberJoinPackageService;
import com.rybbaby.tss.front.tssPertainRelation.entity.TssPertainRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 雷春琼
 * @description 会员表管理
 * @createDate 2017-08-10 上午1:37:58
 */
@Controller
@RequestMapping(value = "tssMemberJoinPakcageController")
public class TssMemberJoinPackageController extends BaseController {

    @Autowired
    private ITssMemberJoinPackageService memberJoinPackageService;


    /**
     * 转到编辑视图
     *
     * @param entity       接收编辑参数的临时对象
     * @param serializeNum 请求标识
     * @return
     */
    @RequestLog(content = "转到会员表视图", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "toEditView", method = RequestMethod.GET)
    public ModelAndView toEditView(TssMemberJoinPackageBean entity, @RequestParam(required = true) String serializeNum, @RequestParam(required = true) String type) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberJoinPackageEditView"));
        List<TssPertainRelation> relationList = new ArrayList<>();
        mav.addObject("editModel", "add");
        mav.addObject("serializeNum", serializeNum);
        mav.addObject("memberType", type);
        mav.addObject("relationList", relationList);
        mav.addObject("saveUrl", StringUtils.substringBeforeLast(request.getRequestURI(), "/") + "/saveOrUpdate.do");
        return mav;
    }

    /**
     * 保存编辑的对象
     *
     * @param entity 被编辑对象
     * @return
     */
    @ResponseBody
    @RequestLog(content = "保存会员表对象", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(TssMemberJoinPackageBean entity) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        String adminUserId = super.getLoginUser().getId();
        try {
            ajr.setResMsg("保存成功!");
            entity.setAdminUserId(adminUserId);
            String[] relationIds=null;
            if (StringUtils.isNotBlank(request.getParameterValues("relationId"))) {
                relationIds= request.getParameterValues("relationId");
            }
            String[] patriarchNames = request.getParameterValues("patriarchName");
            String[] affiliationRelations = request.getParameterValues("affiliationRelation");


            ImmutableMap immutableMap = ImmutableMap.of(("patriarchNames"), patriarchNames, ("affiliationRelations"), affiliationRelations,("relationIds"),relationIds);
            entity.setType(Integer.parseInt(request.getParameter("memberType")));
            //获取课时数
            String classHour=request.getParameter("classHour");
            if(StringUtils.isNotBlank(classHour)){
                entity.setUsableTotalClassHour(Integer.parseInt(classHour+request.getParameter("presentClassHour")));
            }
            boolean bool= this.memberJoinPackageService.save(immutableMap,entity);
            if (!bool) {
                ajr.setResCode(301);
                ajr.setResMsg("保存失败!");
            }else{
                ajr.setResCode(300);
                ajr.setResMsg("保存成功!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }

}
